﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Query;
using HZ.Supervise.Model.Enum;

namespace HZ.Supervise.Web.Company
{
    public partial class CompanyPS_Rate_DZ : BasePage
    {
        public CompanyPS_Rate_DZ()
        {
            this.ModuleNo = Module.履约管理.ToString();
            this.PageOperateType = OperateType.四十八小时配送率.ToString();
        }
        HZ.Supervise.IBLL.IBLL_ProcureCatalog ip = new HZ.Supervise.BLL.BLL_ProcureCatalog();
        IBLL_User iu = BLLFactory.BLL_User();
        KeyOperationLogJG kj; 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                BindData();
            }
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        } 
        protected void BindData()
        {
            try
            {
                int TotalRecord = 0;
                string orderby = "";
                string timeFrom = "1900-01-01 00:00:00";//起始时间
                string timeTo = "2444-12-30 23:59:59";//结束时间
                #region 时间查询
                if (!string.IsNullOrEmpty(txtTimeFrom.Text))
                {
                    timeFrom = txtTimeFrom.Text.Trim();
                }
                if (!string.IsNullOrEmpty(txtTimeTo.Text))
                {
                    timeTo = Convert.ToDateTime(txtTimeTo.Text.Trim()).AddDays(1).ToString();
                }
                if (Convert.ToDateTime(timeFrom) > Convert.ToDateTime(timeTo))
                {
                    HZ.Web.WebUtil.Alert("开始时间不能大于结束时间");
                    return;
                }
                #endregion
                string where = "";
                if (!string.IsNullOrEmpty(txtCompanyName_PS.Text.Trim()))
                {
                    where += string.Format(" AND CompanyName LIKE '%{0}%'", txtCompanyName_PS.Text.Trim());
                }

                DataTable dt = ip.GetCompanyRate_DZ(where,timeFrom,timeTo,orderby, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out TotalRecord);

                AspNetPager1.RecordCount = TotalRecord;
                gvwInfo.DataSource = dt;
                gvwInfo.DataBind();
                AddLog(string.Format("查询48小时配送率低值数据!"));
            }
            catch (Exception e)
            {
                e.ToString();

                HZ.Web.WebUtil.Alert("查询条件请正确填写！");
            }
        } 
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        } 
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            int TotalRecord = 0;
            string orderby = "";
            string timeFrom = "1900-01-01 00:00:00";//起始时间
            string timeTo = "2444-12-30 23:59:59";//结束时间
            #region 时间查询
            if (!string.IsNullOrEmpty(txtTimeFrom.Text))
            {
                timeFrom = txtTimeFrom.Text.Trim();
            }
            if (!string.IsNullOrEmpty(txtTimeTo.Text))
            {
                timeTo = Convert.ToDateTime(txtTimeTo.Text.Trim()).AddDays(1).ToString();
            }
            #endregion
            if (Convert.ToDateTime(timeFrom) > Convert.ToDateTime(timeTo))
            {
                HZ.Web.WebUtil.Alert("开始时间不能大于结束时间");
                return;
            }
            string where = "";
            if (!string.IsNullOrEmpty(txtCompanyName_PS.Text.Trim()))
            {
                where += string.Format(" AND CompanyName LIKE '%{0}%'", txtCompanyName_PS.Text.Trim());
            }
            DataTable dt = ip.GetCompanyRate_DZ(where, timeFrom, timeTo, orderby, 1, 0, out TotalRecord);
            AddLog(string.Format("导出48小时配送率低值数据成功!"));
            ExportFile.Instance.ExportData(dt, gvwInfo, this.Page);
        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg"></param>
        private void AddLog(string msg)
        {
            kj = new KeyOperationLogJG();
            kj.OperationLogSerialID = Guid.NewGuid().ToString();
            kj.Operator = CurrentAccount.GetUser().UserID;
            kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
            kj.OperateTime = DateTime.Now;
            kj.OperateMessage = msg;
            iu.AddKeyOperationLog(kj);
        }
    }
}